import { _decorator, Button, Label, size, Sprite, tween, Tween, UITransform } from 'cc';
import { Ab_BasePopUp_bA } from '../../../A-FRAME/component/ui.pop-up';
import { Ab_ClientAPI_bA } from '../../api/client.api';
import Ab_Config_bA from '../../manager/config.manager';
import Ab_GameDC_bA from '../../manager/data.game.center';
import Ab_GameAssets_bA from '../../manager/game.assets';
import type { Ab_Popup_bA } from '../page/popup';
const { ccclass, property } = _decorator;

@ccclass('Ab_FruitSpecial_bA')
export class Ab_FruitSpecial_bA extends Ab_BasePopUp_bA<typeof Ab_Popup_bA, boolean> {
	protected Ab_default_return_bA: boolean;

	private Ab_light_right_bA: UITransform;
	private Ab_light_left_bA: UITransform;
	private Ab_lbl_symbol_bA: Label;
	private Ab_lbl_diamond_bA: Label;
	private Ab_icon_symbol_bA: UITransform;
	private Ab_icon_diamond_bA: UITransform;

	private Ab_symbol_bA: number;
	private Ab_diamond_bA: number;

	protected Ab_initAfterOnLoad_bA(){
		[this.Ab_light_left_bA, this.Ab_light_right_bA] = ['right', 'left'].map(name=>this.node.getChildByName('light-'+name).getComponent(UITransform));
		[this.Ab_lbl_symbol_bA, this.Ab_lbl_diamond_bA] = ['symbol', 'diamond'].map(name=>this.node.getChildByName('lbl-'+name).getComponent(Label));
		[this.Ab_icon_symbol_bA, this.Ab_icon_diamond_bA] = ['symbol', 'diamond'].map(name=>this.node.getChildByName('icon-'+name).getComponent(UITransform));

		this.Ab_renderLocalUI_bA();
	}

	protected Ab__click_event__bA: { [name: string]: (this: Ab_FruitSpecial_bA, button: Button) => void; } = {
		close(btn){
			btn.interactable = false;
			this.Ab_exit_bA(false).then(()=>btn.interactable = true);
		},
		video(btn){
			btn.interactable = false;
			this.Ab_exit_bA(true).then(()=>btn.interactable = true);
		},
	}

	public Ab_onShow_bA(symbol: number, diamond: number){
		this.Ab_symbol_bA = symbol;
		this.Ab_diamond_bA = diamond;

		Ab_GameDC_bA.Ab_Trigger_bA.Ab_HILIT_SYMBOL_bA = true;
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_HILIT_DIAMOND_bA = true;

		this.Ab_lbl_diamond_bA.string = diamond.toString();
		this.Ab_lbl_symbol_bA.string = Ab_Config_bA.Ab_symbolString_bA(symbol);

		Ab_BasePopUp_bA.Ab_jelly_enter_bA(this.node);
		this.Ab_enter_bA();
	}

	private Ab_enter_bA(){
		[this.Ab_light_right_bA, this.Ab_light_left_bA].forEach(ui=>{
			Tween.stopAllByTarget(ui.node);

			ui.node.angle = 0;
			tween(ui.node)
			.by(1, {angle: 360})
			.repeatForever()
			.start();
		});
	}

	private async Ab_exit_bA(reap: boolean){
		if(reap){
			await Ab_ClientAPI_bA.Ab_AD_bA('incentive');
			if(this.Ab_symbol_bA) Ab_GameDC_bA.Ab_reapSymbol_bA(this.Ab_symbol_bA, this.Ab_icon_symbol_bA.node.worldPosition);
			if(this.Ab_diamond_bA) Ab_GameDC_bA.Ab_reapDiamond_bA(this.Ab_diamond_bA, this.Ab_icon_diamond_bA.node.worldPosition);
		}

		await Ab_BasePopUp_bA.Ab_jelly_leave_bA(this.node);
		this.Ab_close_bA(reap);
		[this.Ab_light_right_bA, this.Ab_light_left_bA].forEach(ui=> Tween.stopAllByTarget(ui.node) );

		Ab_GameDC_bA.Ab_Trigger_bA.Ab_HILIT_SYMBOL_bA = false;
		Ab_GameDC_bA.Ab_Trigger_bA.Ab_HILIT_DIAMOND_bA = false;
	}

	private Ab_renderLocalUI_bA(){
		let ui_title = this.node.getChildByName('title-cong_reap').getComponent(UITransform);
		let title_size = size(
			this.node.getChildByName('background-head').getComponent(UITransform).width,
			ui_title.height
		);

		let sframe = Ab_GameAssets_bA.Ab_getLangTexture_bA(0);

		let scale = Math.min(title_size.width/sframe.originalSize.width, title_size.height/sframe.originalSize.height);

		ui_title.getComponent(Sprite).spriteFrame = sframe;

		ui_title.setContentSize(
			sframe.originalSize.width*scale,
			sframe.originalSize.height*scale
		);

		abi.Ab_cc_bA.Ab_skinPeeler_bA(this.Ab_icon_symbol_bA.node, Ab_GameAssets_bA.Ab_getCounTexture_bA(2), {height: true});

		this.node.getChildByName('lbl-reap').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(17);
		this.node.getChildByName('close').getComponent(Label).string = Ab_GameAssets_bA.Ab_getLangText_bA(18);
	}
}

